package com.outsider.tool.rsa;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class RSATool {
   private char[] askIIMessage;
   private int[] em;
   private char[] message;
   private static Integer[] prime;
   private int p,q,basenum,e,d;
   
		    
   public RSATool(String message){     
	   askIIMessage=message.toCharArray();
	   prime=Prime.getPrime(200);                               //获取素数表
	   do{                                                      //自制random方法 
	   SimpleDateFormat df = new SimpleDateFormat("MMddHHmmss");
	   int random=(int) ((Long.parseLong(df.format(new Date())))%prime.length);
	   p=prime[random];                                        //随机生成p
	   df = new SimpleDateFormat("HHmmssMMdd");
	   random=(int) ((Long.parseLong(df.format(new Date())))%prime.length);
	   q=prime[random];                                       //随机生成q
	   }
	   while (q==p||p*q<130);
	   
	   for (int i=0;i<prime.length;i++){
		   if (((q-1)*(p-1))%prime[i]!=0) {e=prime[i]; break;} //随机生成e 
	   }
	   basenum=q*p;
	   Exgcd exgcd=new Exgcd(e,(p-1)*(q-1));                //通过扩展欧几里得计算d
	   d=exgcd.getX();
	   d=(d%((p-1)*(q-1))+(p-1)*(q-1))%((p-1)*(q-1));
	   

	   System.out.println();
   }
   
   public int[] encrpt(){
	   em=new int[askIIMessage.length];
	   for (int i=0;i<askIIMessage.length;i++){
		   em[i]=(int)(FastPower.getResult(askIIMessage[i],e,basenum) %basenum); //快速幂生成加密信息
	   }
	   
	   return em;
   }
   public char[] decrpt(){
	   message=new char[askIIMessage.length];
	   for (int i=0;i<askIIMessage.length;i++)
		   message[i]=(char)(FastPower.getResult(em[i],d,basenum) %basenum); //快速幂生成解密信息
	   return message;
   } 
   
public char[] getAskIIMessage() {
	return askIIMessage;
}

public int[] getEm() {
	return em;
}

public char[] getMessage() {
	return message;
}

public static Integer[] getPrime() {
	return prime;
}

public int getP() {
	return p;
}

public int getQ() {
	return q;
}

public int getBasenum() {
	return basenum;
}

public int getE() {
	return e;
}

public int getD() {
	return d;
}


   
   
}
